<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Logins extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('auth');
    }

    public function index($message = NULL) {
        $data['message'] = $message;
        $data['title'] = $this->lang->line('webname');
        $data['login_page'] = $this->lang->line('title_login');

        if ($this->auth->log_in()) {
            redirect('homes');
        } else {
            $this->load->view('login_view', $data);
        }
    }

    public function process() {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('inpengguna', $this->lang->line('username'), 'trim|required|min_length[4]|max_length[12]|xss_clean|callback__username_check');
        $this->form_validation->set_rules('insandi', $this->lang->line('password'), 'trim|required|min_length[4]|max_length[12]');

        if ($this->form_validation->run() == FALSE) {
            $this->index();
        } else {
            $inname = $this->input->post('inpengguna');
            $inpass = $this->input->post('insandi');

            if ($this->auth->cek_auth($inname, $inpass)) {
                redirect('homes');
            } else {
                $message = '<div class="alert alert-warning alert-dismissable">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                Invalid ' . $this->lang->line('username') . ' and/or ' . $this->lang->line('password') . '.
                            </div>';
                $this->index($message);
            }
        }
    }

    public function _username_check($str) {
        if ($str == 'admin') {
            $this->form_validation->set_message('_username_check', 'Inputan %s tidak boleh menggunakan kata "admin"');

            return FALSE;
        }
    }

    public function logout() {
        $this->auth->log_out();
        redirect('logins');
    }

}

/* End of file login.php */
/* Location: ./application/controllers/login.php */